/*
    Copyright (C) 2021 Huawei Device Co., Ltd.
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package agency.tango.materialintroscreen.interceptor;

import ohos.agp.components.AttrSet;
import ohos.app.Context;

import agency.tango.materialintroscreen.AttrString;

/**
 * @ProjectName: material-intro-screen-master
 * @Package: agency.tango.materialintroscreen.interceptor
 * @ClassName: AccelerateInterpolator
 * @Description: An interpolator where the rate of change starts out slowly
 * and then accelerates.
 * @CreateDate: 2021/4/12 16:18
 * @UpdateDate: 2021/4/12 16:18
 * @UpdateRemark: 暂无
 * @Version: 1.0
 */

public class AccelerateInterpolator extends BaseInterpolator {
    private final float mFactor;
    private final double mDoubleFactor;

    public AccelerateInterpolator() {
        mFactor = 1.0f;
        mDoubleFactor = 2.0;
    }

    /**
     * Constructor
     *
     * @param factor Degree to which the animation should be eased. Seting
     *               factor to 1.0f produces a y=x^2 parabola. Increasing
     *               factor above
     *               1.0f  exaggerates the ease-in effect (i.e., it starts even
     *               slower and ends evens faster)
     */
    public AccelerateInterpolator(float factor) {
        mFactor = factor;
        mDoubleFactor = 2 * mFactor;
    }

    public AccelerateInterpolator(Context context, AttrSet attrs) {
        mFactor =
                attrs.getAttr(AttrString.IACCELERATEINTERPOLATOR_FACTOR).isPresent() ?
                        attrs.getAttr(AttrString.IACCELERATEINTERPOLATOR_FACTOR).get().getFloatValue() : 1.0F;
        mDoubleFactor = 2 * mFactor;
        setChangingConfiguration(0);
    }

    /**
     * 获取插值
     *
     * @param input A value between 0 and 1.0 indicating our current point
     *              in the animation where 0 represents the start and 1.0 represents
     *              the end
     * @return 插值
     */
    public float getInterpolation(float input) {
        return getCurvedTime(input);
    }

    @Override
    public float getCurvedTime(float input) {
        if (mFactor == 1.0f) {
            return input * input;
        } else {
            return (float) Math.pow(input, mDoubleFactor);
        }
    }
}
